﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace HQWeb.Progect.ProgectManage
{
    public partial class ProgectEdit : BasePage
    {
        private Boolean flag;
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BianBranchList();
                BindType();
                flag = Convert.ToBoolean(Session["IsAll"]);
                if (flag)            //是否可以管理所有分店用户
                {

                    div1.Style["display"] = "";
                }
                else
                {
                    div2.Style["display"] = "";
                }
                if (Request["from"]!=null)
                {
                    hidFrom.Value = Request["from"].Trim();
                }
                CheckState();
            }
        }

        #region 判断进入状态
      
        private void CheckState()
        {
            if (Request["id"] != null)   //编辑
            {
                hidType.Value = "edit";
                string strID = Request["id"].Trim();
                this.hidPid.Value = strID;
                FillPage(strID);
            }
            else
            {              //添加
                if (!flag)         //分店所属分店写死自己分店无需选择
                {
                    BLL.SysManage.T_PUB_BRANCH bll = new BLL.SysManage.T_PUB_BRANCH();
                    int temp;
                    int.TryParse(Session["BRANCH_ID"].ToString(), out temp);
                    Model.SysManage.T_PUB_BRANCH model = bll.GetModel(temp);
                    this.labBranch.Text = model.BRANCH_NAME;
                    dropBranch.SelectedValue = Session["BRANCH_ID"].ToString();
                }
                else
                {
                    dropBranch.SelectedValue = Session["BRANCH_ID"].ToString();
                }
                if (Request["TypeID"] != null)      //项目分类
                {
                    string strType = Request["TypeID"].ToString();
                    if (dropType.Items.FindByValue(strType) != null)
                    {
                        dropType.SelectedValue = strType;
                    }
                }
            }
        }

        #endregion

        #region 编辑时填充项目数据页面


        private void FillPage(string strID)
        {

            #region 绑定项目材料

            BLL.Progect.T_PUB_PROJECTMATERIAL bll = new BLL.Progect.T_PUB_PROJECTMATERIAL();
            DataTable modelList = bll.GetModelListByPid(strID).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralNamel;
            string strNum;
            string strRemark;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralNamel = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();
                strRemark = modelList.Rows[i]["REMARK"].ToString();

                strHtml.Append("  <table width=\"90%\" id=\"table" + i + "\">");
                strHtml.Append(" <hr />");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:20%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\">");
                strHtml.Append(" <input type=\"text\" id=\"y" + i + "\" value=\"" + strMateralNamel + "\"/><input type=\"button\" id=\"btny" + i + "\" value=\"选择\"  onclick=\"openTreeSelect('d" + i + "','y" + i + "','')\" />");
                strHtml.Append(" <input type=\"text\" id=\"d" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr>");
                strHtml.Append("<td align=\"right\">");
                strHtml.Append("数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\">");
                strHtml.Append("<input type=\"text\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                strHtml.Append(" </tr>");
                strHtml.Append("<tr>");
                strHtml.Append(" <td align=\"right\">");
                strHtml.Append("说明：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" colspan=\"2\">");
                strHtml.Append("<input type=\"text\" id=\"b" + i + "\"  style=\"width: 400px\"value=\"" + strRemark + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr align=\"left\">");
                strHtml.Append("<td colspan=\"4\">");
                strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除该元素\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                strHtml.Append("</td>");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();

            #endregion

            if (!flag)                                                 //是否可以管理所有分店用户 
            {
                BLL.SysManage.T_PUB_BRANCH Brbll = new BLL.SysManage.T_PUB_BRANCH();
                Model.SysManage.T_PUB_BRANCH Brmodel = Brbll.GetModel(Convert.ToInt32(Session["BRANCH_ID"].ToString()));
                labBranch.Text = Brmodel.BRANCH_NAME;
            }
            //else
            //{
            //    if (hidBranch.Value != "")
            //    {
            //        dropBranch.SelectedValue = hidBranch.Value;
            //    }
            //}
        }
        #endregion

        #region 绑定下拉列表框

        private void BianBranchList()
        {
            BLL.SysManage.T_PUB_BRANCH branchBll = new BLL.SysManage.T_PUB_BRANCH();
            List<Model.SysManage.T_PUB_BRANCH> branchModel = branchBll.GetModelList("");
            dropBranch.DataSource = branchModel;
            dropBranch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            dropBranch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            dropBranch.DataBind();

        }
        private void BindType()
        {
            BLL.SysManage.T_PUB_PROJECTTYPE Bll = new BLL.SysManage.T_PUB_PROJECTTYPE();

            List<Model.SysManage.T_PUB_PROJECTTYPE> list = Bll.GetModelList("");
            dropType.DataSource = list;
            dropType.DataTextField = "PROJECTTYPE_NAME"; //dropdownlist的Text的字段 
            dropType.DataValueField = "PROJECTTYPE_ID";//dropdownlist的Value的字段 
            dropType.DataBind();
        }

        #endregion

    }
}